#include <iostream>
using namespace std;
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstring>
#include <arpa/inet.h>
#include <cstdlib>
#include "stdShared.h"
#include <json-c/json.h>
#define PORT 8889
#define BUFFER_SIZE 64

struct Msg
{

    char m_name[BUFFER_SIZE];
    char m_passwd[BUFFER_SIZE];
    int m_type; // 业务类型
};

void menu()
{
    system("clear");
    cout << "1.注册" << endl;
    cout << "2.登录" << endl;
    cout << "3.退出" << endl;
}

void registerFunc(int sockfd)
{
    char username[BUFFER_SIZE];
    cout << "请输入用户名" << endl;
    scanf("%s", username);

    char passwd[BUFFER_SIZE];
    cout << "请输入密码" << endl;
    scanf("%s", passwd);

    /*创建json对象*/
    json_object *jsonObj = json_object_new_object();
    json_object_object_add(jsonObj, "type", json_object_new_int(REGISTER));
    json_object_object_add(jsonObj, "username", json_object_new_string(username));
    json_object_object_add(jsonObj, "passwd", json_object_new_string(passwd));

    /*将json对象转换成字符串*/
    const char *jsonStr = json_object_to_json_string(jsonObj);

    /*发送信息到服务器*/
    write(sockfd, jsonStr, strlen(jsonStr));
    // memset(data, 0, sizeof(data));
    // /*接受服务器发送回的消息*/
    // read(sockfd, data, sizeof(data));
    // cout << "服务器发过来的数据是：" << data << endl;
}

void loginFunc(int sockfd)
{
    char username[BUFFER_SIZE];
    cout << "请输入用户名" << endl;
    scanf("%s", username);

    char passwd[BUFFER_SIZE];
    cout << "请输入密码" << endl;
    scanf("%s", passwd);

    /*创建json对象*/
    json_object *jsonObj = json_object_new_object();
    json_object_object_add(jsonObj, "type", json_object_new_int(LOGIN));
    json_object_object_add(jsonObj, "username", json_object_new_string(username));
    json_object_object_add(jsonObj, "passwd", json_object_new_string(passwd));

    /*将json对象转换成字符串*/
    const char *jsonStr = json_object_to_json_string(jsonObj);

    /*发送信息到服务器*/
    
    write(sockfd, jsonStr, strlen(jsonStr));
}

int main()
{
    /*创建通信文件描述符*/
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket create error");
        _exit(-1);
    }

    /*连接服务器*/
    sockaddr_in serverAddress;
    /*地址族IPV4*/
    serverAddress.sin_family = AF_INET;
    /*端口*/
    serverAddress.sin_port = htons(PORT);
    /*服务器ip地址*/
    const char *servereIp = "192.168.85.129";
    // 将 servereIp 中的 IPv4 地址（例如 "192.168.1.1"）转换成网络字节序的 32 位整数，
    // 并将其存储在 serverAddress.sin_addr.s_addr 中
    inet_pton(AF_INET, servereIp, &(serverAddress.sin_addr.s_addr));
    /*连接服务器*/
    connect(sockfd, (const sockaddr *)&serverAddress, sizeof(serverAddress));
    /*客户端发送信息给服务器 发送helloworld 服务器接收信息，回复HELLOWORLD*/
    // char buffer[BUFFER_SIZE];
    // cin >> buffer;
    // write(sockfd, buffer, strlen(buffer));

    char data[BUFFER_SIZE] = {0};
    while (1)
    {
        memset(data, 0, sizeof(data));
        menu();
        cout << "请输入:";
        int choice;
        cin >> choice;
        switch (choice)
        {
        case 1:
            registerFunc(sockfd);
            break;
        case 2:
            loginFunc(sockfd);
            break;
        case 3:
            /*todo...*/
            exit(0);
            break;
        default:
            break;
        }
    }
    return 0;
}
